import { createSlice,createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";

export const fetchHomeMultidataAction = createAsyncThunk('fetch', async () => {
  const res = await axios.get('http://123.207.32.32:8000/home/multidata');
  return res.data;
})

const homeSlice = createSlice({
  name: 'home',
  initialState: {
    banners: []
  },
  reducers: {
    changeBanner(state, { payload }) {
      state.banners = payload;
    }
  },
  extraReducers: (builder) => {
    builder.addCase(fetchHomeMultidataAction.fulfilled, (state, { payload }) => {
      console.log("异步操作:", state, payload);
      state.banners = payload.data.banner.list;
    }).addCase(fetchHomeMultidataAction.pending, (state, { payload }) => {
      console.log("pending");
    }).addCase(fetchHomeMultidataAction.rejected, (state, { payload }) => {
      console.log("rejected");
    })
  }
})

export default homeSlice.reducer;
export const { changeBanner } = homeSlice.actions;